package org.jeecg.modules.clue.job;


import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.clue.entity.ClueBatchQuery;
import org.jeecg.modules.clue.entity.ClueTaskQueryLog;
import org.jeecg.modules.clue.enums.BatchQueryStatStatus;
import org.jeecg.modules.clue.enums.ClueQueryStatus;
import org.jeecg.modules.clue.service.IClueBatchQueryService;
import org.jeecg.modules.clue.service.IClueTaskQueryLogService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Component
@Slf4j
@DisallowConcurrentExecution
public class ClueBatchQueryCheckJob implements Job {

    /**
     * 若参数变量名修改 QuartzJobController中也需对应修改
     */
    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }


    @Resource
    IClueBatchQueryService clueBatchQueryService;
    @Resource
    IClueTaskQueryLogService clueTaskQueryLogService;
    @Resource(name = "clueMailAccount")
    private MailAccount mailAccount;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("ClueBatchQueryCheckJob 定时任务");
        List<ClueBatchQuery> batchQueryList = clueBatchQueryService.list(new LambdaQueryWrapper<ClueBatchQuery>()
                .eq(ClueBatchQuery::getStatStatus, BatchQueryStatStatus.未统计.status)
        );

        for (ClueBatchQuery batchQuery : batchQueryList) {
            List<ClueTaskQueryLog> waitQueryList = clueTaskQueryLogService.list(
                    new LambdaQueryWrapper<ClueTaskQueryLog>().eq(ClueTaskQueryLog::getBatchQueryNo, batchQuery.getBatchNo())
                            .eq(ClueTaskQueryLog::getQueryStatus, ClueQueryStatus.待查询.status)
            );
            if (waitQueryList.isEmpty()) { // 说明任务已经执行完毕
                List<ClueTaskQueryLog> successQueryList = clueTaskQueryLogService.list(
                        new LambdaQueryWrapper<ClueTaskQueryLog>().eq(ClueTaskQueryLog::getBatchQueryNo, batchQuery.getBatchNo())
                                .eq(ClueTaskQueryLog::getQueryStatus, ClueQueryStatus.查询成功.status)
                );


                List<ClueTaskQueryLog> failQueryList = clueTaskQueryLogService.list(
                        new LambdaQueryWrapper<ClueTaskQueryLog>().eq(ClueTaskQueryLog::getBatchQueryNo, batchQuery.getBatchNo())
                                .eq(ClueTaskQueryLog::getQueryStatus, ClueQueryStatus.查询失败.status)
                );
                batchQuery.setStatStatus(BatchQueryStatStatus.已统计.status);
                batchQuery.setQuerySuccess(BigDecimal.valueOf(successQueryList.size()));
                batchQuery.setQueryFail(BigDecimal.valueOf(failQueryList.size()));
                batchQuery.setCompleteTime(new Date());
                clueBatchQueryService.updateById(batchQuery);


                for (String email : Arrays.asList("1150852308@qq.com", mailAccount.getUser())) {
                    String send = Mail.create(mailAccount)
                            .setTos(email)
                            .setTitle("批次查询统计结果通知")
                            .setContent(String.format("统计时间:%s  批次查询记录号:%s  成功数量:%s   失败数量:%s   统计状态:%s  开始时间:%s  完成时间:%s", DateUtils.now(),batchQuery.getBatchNo(),successQueryList.size(),failQueryList.size(),"已统计", DateUtil.format(batchQuery.getCreateTime(),"yyyy-MM-dd HH:mm:ss"),DateUtil.format(batchQuery.getCompleteTime(),"yyyy-MM-dd HH:mm:ss") ))
                            .send();
                    log.info("邮件发送结果 {}", send);
                }

            }
        }

    }
}
